using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.HtmlControls;

namespace SurveyHost.Core.Controls
{
    [ToolboxItem(false)]
    [ValidationProperty("AnswerText")]
    class TextboxQuestion : BaseSurveyQuestion
    {
        /// <summary>
        /// Help text that can be displayed to users to instruct them how to generate a configuration string.
        /// </summary>
        /// <value></value>
        /// <remarks>Eventually will be replaced by a custom web control used to generate the configuration string.</remarks>
        public override string ConfigurationHelpText
        {
            get { return "This question type allows users to enter any text value.<br/>Answers have a maximum length of 100 characters.  To restrict the length of the answer, enter the maximum allowed characters."; }
        }

        private TextBox textBox;

        /// <summary>
        /// The response from the user
        /// </summary>
        /// <value></value>
        /// <remarks>
        /// AnswerText as null indicates the user refused to answer.
        /// </remarks>
        public override string AnswerText
        {
            get
            {
                return textBox == null ? string.Empty : textBox.Text;
            }
            set
            {
                if (textBox == null)
                    CreateTextBox(100);
                textBox.Text = value;
            }
        }
        
        /// <summary>
        /// Creates the textbox and limits its input to the given length
        /// </summary>
        /// <param name="length">Max length of input for this textbox</param>
        private void CreateTextBox(int length)
        {
            textBox = new TextBox();
            textBox.MaxLength = length;
        }

        /// <summary>
        /// Called by the base class at the time it is appropriate to parse the configuration string,
        /// The configuration can be stored or used to create controls.
        /// </summary>
        protected override void ParseConfigurationString()
        {
            base.ParseConfigurationString();
            int max = -1;
            if (!int.TryParse(ConfigurationString, out max) || max < 1 || max > 100)
                max = 100;
            CreateTextBox(max);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            ParseConfigurationString();

            HtmlGenericControl questionText = new HtmlGenericControl(HtmlTextWriterTag.Div.ToString());
            HtmlGenericControl questionAnswer = new HtmlGenericControl(HtmlTextWriterTag.Div.ToString());
            questionText.Attributes.Add(HtmlTextWriterAttribute.Class.ToString(), "questionText");
            questionAnswer.Attributes.Add(HtmlTextWriterAttribute.Class.ToString(), "questionAnswer");
            Controls.Add(questionText);
            Controls.Add(questionAnswer);

            LiteralControl literal = new LiteralControl();
            questionText.Controls.Add(
                new LiteralControl(
                    string.Format(QuestionLineFormat, QuestionIndex, QuestionText)
                    ));

            textBox.CssClass = "questionTb";
            textBox.MaxLength = 500;
            questionAnswer.Controls.Add(textBox);
        }
    }
}
